package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.Bank;

import javax.script.ScriptException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *流程放款信息表
 */
@BusinessDataConverter(
      insertSql = "INSERT INTO ND_LOAN_INFORMATION_FORM\n" +
          "(ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, PRO_ID, GUARANTEE_CONTRACT_NUMBER, LOAN_CONTRACT_NUMBER, CONTRACT_START_TIME, CONTRACT_EXPIRATION_TIME, ACTUAL_LOAN_AMOUNT, REPAYMENT_INTEREST_RATE, LOAN_TIME, LOAN_EXPIRATION_TIME, LOAN_REVIEW_TIME, ACTUAL_LENDING_BANK, BANK_BRANCH_NAME, APPLICATION_AMOUNT, GUARANTEED_CONTRACT_ID, GUARANTEED_CONTRACT_AMOUNT, PROC_ID, IS_VALID, TRREMARK, LOANVOUCHER_ID, REAL_ANNUAL_INTEREST, REVIEW_MARK, LOAN_NOTIFICATION_ID, LENDING_NOTICE_NUMBER, LOAN_NOTIFICATION_CREATE_DATE, PAYACCOUNTNO, SIGNEDORNOT, IS_COVERAGE)\n" +
          "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
      updateSql = "UPDATE ND_LOAN_INFORMATION_FORM\n" +
          "SET CREATE_NAME=?, CREATE_BY=?, CREATE_DATE=?, UPDATE_NAME=?, UPDATE_BY=?, UPDATE_DATE=?, SYS_ORG_CODE=?, SYS_COMPANY_CODE=?, BPM_STATUS=?, PRO_ID=?, GUARANTEE_CONTRACT_NUMBER=?, LOAN_CONTRACT_NUMBER=?, CONTRACT_START_TIME=?, CONTRACT_EXPIRATION_TIME=?, ACTUAL_LOAN_AMOUNT=?, REPAYMENT_INTEREST_RATE=?, LOAN_TIME=?, LOAN_EXPIRATION_TIME=?, LOAN_REVIEW_TIME=?, ACTUAL_LENDING_BANK=?, BANK_BRANCH_NAME=?, APPLICATION_AMOUNT=?, GUARANTEED_CONTRACT_ID=?, GUARANTEED_CONTRACT_AMOUNT=?, PROC_ID=?, IS_VALID=?, TRREMARK=?, LOANVOUCHER_ID=?, REAL_ANNUAL_INTEREST=?, REVIEW_MARK=?, LOAN_NOTIFICATION_ID=?, LENDING_NOTICE_NUMBER=?, LOAN_NOTIFICATION_CREATE_DATE=?, PAYACCOUNTNO=?, SIGNEDORNOT=?, IS_COVERAGE=?\n" +
          "WHERE ID = ?",
      findMigratedIdListSql = "SELECT ID FROM ND_LOAN_INFORMATION_FORM WHERE PRO_ID = ?"
)
public class NdLoanInformationForm extends AbstractBusiness {

  public NdLoanInformationForm(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
    super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
  }

  @Override
  public void migrate() throws Exception {
    for (Map btItemAccountDetail : this.sourceData.getBtItemAccountDetailList()) {
      if(!"1".equals(btItemAccountDetail.get("ACCOUNTTYPE").toString())){
        continue;
      }
      // 判断是否需要覆盖迁移
      int needMigratedResult = this.needMigrated(btItemAccountDetail.get("ACCOUNTNO").toString());
      if(needMigratedResult == 0) {
        continue;
      }

      // 单条插入或更新
      if(needMigratedResult == 1) {
        this.targetDbUtil.updateByParams(updateSql,this.buildValues(btItemAccountDetail),true);
      } else {
        this.targetDbUtil.updateByParams(insertSql,this.buildValues(btItemAccountDetail));
      }
    }
  }

  public List buildValues(Map row) throws ScriptException, SQLException {
    List values = new ArrayList();
    //ID	主键
    values.add(row.get("ACCOUNTNO").toString());
    //CREATE_NAME	创建人名称
    values.add(null);
    //CREATE_BY	创建人登录名称
    values.add(null);
    //CREATE_DATE	创建日期
    values.add(row.get("CREATE_TIME"));
    //UPDATE_NAME	更新人名称
    values.add(null);
    //UPDATE_BY	更新人登录名称
    values.add(null);
    //UPDATE_DATE	更新日期
    values.add(row.get("MODIFY_TIME"));
    //SYS_ORG_CODE	所属部门
    values.add(null);
    //SYS_COMPANY_CODE	所属公司
    values.add("002");
    //BPM_STATUS	流程状态
    values.add(1);
    //PRO_ID	项目ID
    values.add(this.projectId);

    //GUARANTEE_CONTRACT_NUMBER	保证合同编号
    values.add(row.get("PROMCONTRACTNO"));
    //LOAN_CONTRACT_NUMBER	借款合同编号
    values.add(row.get("LOANCONTRACTNO"));
    //CONTRACT_START_TIME	保证合同开始时间
    values.add(null);
    //CONTRACT_EXPIRATION_TIME	保证合同到期时间
    values.add(null);

    //    ACTUAL_LOAN_AMOUNT	实际放款金额	16	NUMBER(20,6)	[NULL]	false	[NULL]
    values.add(row.get("OCCURSUM"));
    //    REPAYMENT_INTEREST_RATE	还款年息利率	17	NUMBER(20,6)	[NULL]	false	[NULL]
    values.add(row.get("YEARRATE"));
    //    LOAN_TIME	放款发生时间	18	DATE	[NULL]	false	[NULL]
    values.add(Tool.sqlTimestampToSqlDate(row.get("OCCURTIME")));
    //    LOAN_EXPIRATION_TIME	放款到期时间	19	DATE	[NULL]	false	[NULL]
    values.add(Tool.sqlTimestampToSqlDate(row.get("ENDTIME")));
    //    LOAN_REVIEW_TIME	放款复核时间	20	DATE	[NULL]	false	[NULL]
    values.add(null);
    //    ACTUAL_LENDING_BANK	实际放款银行	21	VARCHAR2(32)	[NULL]	false	[NULL]
    values.add(DictTool.getTargetDictCode(Bank.class,row.get("BANKID")));
    //    BANK_BRANCH_NAME	银行支行名称	22	VARCHAR2(50)	[NULL]	false	[NULL]
    values.add(row.get("BANKBRANCH"));
    //    APPLICATION_AMOUNT	本次申请放款金额	23	NUMBER(20,6)	[NULL]	false	[NULL]
    values.add(row.get("OCCURSUM"));
    //    GUARANTEED_CONTRACT_ID	保证合同信息ID	24	VARCHAR2(36)	[NULL]	false	[NULL]
    if(row.get("PROMCONTRACTNO") == null) {
      values.add(null);
    } else {
      values.add(MD5Utils.stringToMD5(row.get("PROMCONTRACTNO").toString()+"-"+this.projectId+"-1"));
    }
    //    GUARANTEED_CONTRACT_AMOUNT	签订保证合同金额	25	NUMBER(20,6)	[NULL]	false	[NULL]
    Object guaranteedContractAmount = row.get("guaranteedContractAmount");
    if(guaranteedContractAmount == null) {
      guaranteedContractAmount = BusinessTool.getGuaranteeContractAmount(row.get("PROMCONTRACTNO").toString(),this.sourceData);
    }
    values.add(guaranteedContractAmount);
    //    PROC_ID	流程ID	26	VARCHAR2(36)	[NULL]	false	[NULL]
    values.add( row.get("WORKFLOWNO") != null ? row.get("WORKFLOWNO").toString()+ "-JW" : null);
    //    IS_VALID	是否使用	27	VARCHAR2(32)	[NULL]	false	[NULL]
    values.add("Y");
    //    TRREMARK	说明	28	VARCHAR2(500)	[NULL]	false	[NULL]
    values.add(Tool.decodeUnicode(row.get("ACCOUNTMEMO")));
    //    LOANVOUCHER_ID	放款凭证id	29	VARCHAR2(36)	[NULL]	false	[NULL]
    values.add(row.get("ELEFILESTORENAME") == null ? null : row.get("ELEFILESTORENAME").toString().replace("-",""));
    //    REAL_ANNUAL_INTEREST	实际年利息率	30	NUMBER(20,6)	[NULL]	false	[NULL]
    values.add(row.get("YEARRATE"));
    //    REVIEW_MARK	复核标记	31	VARCHAR2(32)	[NULL]	false	[NULL]
    // 0: 待审核
    // 1：审核不通过
    // 2：审核通过
    values.add(row.get("sencorType") != null ? "0":"2");
    //    LOAN_NOTIFICATION_ID	放款通知书ID	32	VARCHAR2(36)	[NULL]	false	[NULL]
    values.add(null);
    //    LENDING_NOTICE_NUMBER	放款通知书编号	33	VARCHAR2(36)	[NULL]	false	[NULL]
    values.add(null);
    //    LOAN_NOTIFICATION_CREATE_DATE	放款通知书创建时间	34	DATE	[NULL]	false	[NULL]
    values.add(null);
    //    PAYACCOUNTNO	银行卡号	35	VARCHAR2(100)	[NULL]	false	[NULL]
    values.add(row.get("CLIENTBANKACCOUNT"));
    //    SIGNEDORNOT	是否签章	36	VARCHAR2(128)	[NULL]	false	[NULL]
    values.add(null);
    //    IS_COVERAGE		37	NUMBER(2,0)	[NULL]	false	"0"
    values.add(null);

    return values;
  }

  public void customMigrate(Map row) throws Exception {

    // 判断是否需要覆盖迁移
    int needMigratedResult = this.needMigrated(row.get("ACCOUNTNO").toString());
    if(needMigratedResult == 0) {
      return;
    }

    // 单条插入或更新
    if(needMigratedResult == 1) {
      this.targetDbUtil.updateByParams(updateSql,this.buildValues(row),true);
    } else {
      this.targetDbUtil.updateByParams(insertSql,this.buildValues(row));
    }
  }

}


